package com.panshi.controller.question;

import com.panshi.api.QuestionAuditApi;
import com.panshi.api.UserApi;
import com.panshi.bean.PageBean;
import com.panshi.po.QuestionAuditPO;
import com.panshi.po.QuestionPO;
import com.panshi.po.SortPO;
import com.panshi.po.UserPO;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@Slf4j
public class QuestionAuditController {

    //注入问题审核接口
    @Autowired
    QuestionAuditApi questionAuditApi;

    @Autowired
    UserApi userApi;


    @GetMapping("questionAudit-home")
    public String queryHome(){
        return "questionAudit/question-audit";
    }

    @PostMapping("questionAudit-queryAll")
    @ResponseBody
    public PageModel<QuestionAuditPO> queryAll(PageModel<QuestionAuditPO> pageModel){
        PageBean pageVO = new PageBean();
        pageVO.setCurrentPageSize(pageModel.getPageSize());
        pageVO.setCurrentPage(pageModel.getCurrentPage());
        List<QuestionAuditPO> list=questionAuditApi.findQuestionByAll(pageVO).getData();
        Integer integer=questionAuditApi.findAllCount();
        pageModel.setList(list);
        pageModel.setCount(integer);
        return pageModel;
    }


    @PostMapping("questionAudit-limitQuery/{vague}")
    @ResponseBody
    public PageModel<QuestionAuditPO> limitQuery(@PathVariable String vague, PageModel<QuestionAuditPO> pageModel){
        log.info("进来了=================={}",vague);
        PageBean pageVO = new PageBean();
        pageVO.setCurrentPageSize(pageModel.getPageSize());
        pageVO.setCurrentPage(pageModel.getCurrentPage());

        QAFindByVagueVO qAFindByVagueVO = new QAFindByVagueVO();
        qAFindByVagueVO.setVague(vague);
        qAFindByVagueVO.setCurrentPage(pageVO.getCurrentPage());
        qAFindByVagueVO.setCurrentPageSize(pageVO.getCurrentPageSize());

        List<QuestionAuditPO> list =questionAuditApi.findQuestionByVague(qAFindByVagueVO).getData();
        Integer integer=questionAuditApi.findByVagueCount(vague);
        pageModel.setList(list);
        pageModel.setCount(integer);
        return pageModel;

    }

    //根据审核问题id查询问题信息
    @GetMapping("questionAudit-update/{id}")
    @ResponseBody
    public ModelAndView limitQuery(@PathVariable Integer id){
        ModelAndView mv = new ModelAndView("questionAudit/questionAudit-edit");
        //问题信息
        QuestionAuditPO questionAuditPO=questionAuditApi.findQuestionById(id).getData();

        //获取用户名称
        UserPO userPO =userApi.findUserById(questionAuditPO.getUid()).getData();
        log.info("questionAuditPO====={}",questionAuditPO);
        log.info("userPo====={}",userPO);

        mv.addObject("questionAuditPO",questionAuditPO);
        mv.addObject("userPO",userPO);
        return mv;
    }


    @PostMapping("questionAudit-audit")
    @ResponseBody
    public ResponseVO audit(QuestionAuditUpdateVO questionAuditUpdateVO){
        questionAuditUpdateVO.setDate(new Date());
        Integer integer=questionAuditApi.questionAudit(questionAuditUpdateVO).getData();
        if(integer == 1){
            return new ResponseVO("200","修改成功",true);
        }
        return new ResponseVO("500","修改失败",false);
    }



}
